package com.cssw.fyzb.core.dao.tesk;

import com.cssw.fyzb.core.model.dodb.TaskExec;
import com.cssw.fyzb.core.util.DateUtil;
import com.ydn.dbframe.plugin.activerecord.Db;
import com.ydn.dbframe.plugin.activerecord.Page;
import com.ydn.dbframe.plugin.activerecord.parse.SqlParse;
import org.springframework.stereotype.Repository;

import java.util.*;

/**
 * @author
 */
@Repository
public class TaskExecDaoImpl implements TaskExecDao {

    @Override
    public Long save(TaskExec taskExec) {
        boolean save = taskExec.save();
        return taskExec.getExecId();
    }

    @Override
    public List<TaskExec> list(Long userId, String status) {
        //超过48（2天）时过期
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DATE, -2);
        Date time = cal.getTime();
        Map<String, Object> map = new HashMap<>();
        map.put("oldDate", time);
        map.put("status", status);
        map.put("userId", userId);
        SqlParse sqlParse = new SqlParse(map);
        sqlParse.addSQL("select t1.* from t_task_exec t1 ");
        sqlParse.addSQL(" where t1.user_id =:userId and t1.status=:status and create_time>:oldDate ");
        return TaskExec.dao.find(sqlParse);
    }

    @Override
    public Page<TaskExec> listTaskExec(Long userId, String status, int pageNumber, int pageSize) {
        Map<String, Object> map = new HashMap<>();
        map.put("userId", userId);
        if ("2".equals(status)) {
            map.put("status", status);
        } else if ("1".equals(status)) {
            map.put("status", status);
            Calendar cal = Calendar.getInstance();
            cal.add(Calendar.DATE, -2);
            Date time = cal.getTime();
            map.put("oldDate", DateUtil.format(time));
        } else { //过期
            Calendar cal = Calendar.getInstance();
            cal.add(Calendar.DATE, -2);
            Date time = cal.getTime();
            map.put("oldDate", DateUtil.format(time));
            map.put("status", "2");
        }
        SqlParse sqlParse = new SqlParse(map);
        sqlParse.addSQL(" select t1.*,  ");
        sqlParse.addSQL(" (select t3.task_name from t_rule_inst t2 left join d_task t3 on t2.task_id=t3.task_id where t2.inst_id=t1.inst_id  ) task_name");
        sqlParse.addSQL(" from t_task_exec t1 where user_id=:userId ");
        if ("3".equals(status)) {
            sqlParse.addSQL(" and create_time<:oldDate  ");
            sqlParse.addSQL(" and status!=:status ");
        } else {
            sqlParse.addSQL(" and status=:status ");
        }
        if ("1".equals(status)) {
            sqlParse.addSQL(" and create_time>:oldDate  ");
        }
        return TaskExec.dao.paginate(pageNumber, pageSize, sqlParse);
    }

    @Override
    public TaskExec getByCond(Long userId, String dayTime, Long instId) {
        String sql = "select * from t_task_exec where user_id = ? and day_time = ? and inst_id = ?";
        return TaskExec.dao.findFirst(sql, userId, dayTime, instId);
    }

    @Override
    public TaskExec getTaskExec(Long userId, Long instId) {
        String sql = "select * from t_task_exec where user_id = ? and inst_id=? and day_time = ?";
        return TaskExec.dao.findFirst(sql, userId, instId, DateUtil.parseMbl(new Date()));
    }

    @Override
    public void updateStatus(Long userId, Long execId, String status) {
        StringBuffer str = new StringBuffer();
        str.append(" update t_task_exec set status=? where user_id=? and exec_id=?");
        Db.update(str.toString(), status, userId, execId);
    }

}
